﻿/***************************************************************************
 * 文件名：MainForm
 * 功能：
 * 说明： 常量类生成
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/9 08:50:43
 * 创建人：葛浩
 * 邮箱： 2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using DevExpress.XtraEditors;
using System;
using System.IO;
using System.Windows.Forms;
using Util;

namespace ConstClassGenerator
{
	public partial class MainForm : XtraForm
	{
		#region Propertys

		private ConstGenerator _ConstGenerator;

		#endregion

		#region Const

		/// <summary>
		/// 默认常量名称
		/// </summary>
		private const string c_DefaultConstClassName = "XXXConst";

		#endregion

		#region Win

		public MainForm()
		{
			InitializeComponent();
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			_ConstGenerator = new ConstGenerator();
		}

		#endregion

		#region Methods


		#endregion

		#region Event

		/// <summary>
		/// 生成
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnGenerator_Click(object sender, System.EventArgs e)
		{
			try
			{
				string constClassGeneratorTarget = txtConstClassGeneratorTarget.Text;
				if (string.IsNullOrEmpty(constClassGeneratorTarget))
				{
					MessageBox.Show("请输入常量类生成的字符串", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				string constClassName = txtConstClassName.Text;
				if (string.IsNullOrEmpty(constClassName))
				{
					MessageBox.Show("请输入常量类名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				constClassName = constClassName + (!constClassName.Equals(c_DefaultConstClassName) ? "Const" : "");

				txtConstClassGeneratorResult.Text = _ConstGenerator.ConstClassGeneratorExecute(constClassGeneratorTarget, constClassName);
			}
			catch (Exception ex)
			{
				MessageBox.Show($"生成常量类失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 清空
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClear_Click(object sender, System.EventArgs e)
		{
			txtConstClassGeneratorResult.Text = string.Empty;
		}
		
		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSaave_Click(object sender, System.EventArgs e)
		{
			try
			{
				string constClassGeneratorResult = txtConstClassGeneratorResult.Text;
				if (string.IsNullOrEmpty(constClassGeneratorResult))
				{
					MessageBox.Show("请先生成常量类。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				SaveFileDialog saveFileDialog = new SaveFileDialog
				{
					Filter = "txt文件 (*.txt)|*.txt",
					DefaultExt = ".txt",
					Title = "保存文件"
				};

				DialogResult dialogResult = saveFileDialog.ShowDialog();
				if (dialogResult != DialogResult.OK)
				{
					return;
				}

				string filePath = saveFileDialog.FileName;

				File.WriteAllText(filePath, constClassGeneratorResult);

				MessageBox.Show("保存成功!。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show($"保存失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

	}
}
